#pragma semicolon 1
#pragma newdecls required

#include <sourcemod>
#include <sdktools>

#define PLUGIN_VERSION "1.0"

char g_sZombieClass[][] = 
{
	"Smoker",
	"Boomer",
	"Hunter",
	"Spitter",
	"Jockey",
	"Charger",
	"witch",
	"Tank"
};

char g_sZombieName[][] = 
{
	"Smoker",
	"Boomer",
	"Hunter",
	"Spitter",
	"Jockey",
	"Charger",
	"witch",
	"Tank"
};

ConVar C_KillSwitch;

public Plugin myinfo =
{
	name = "l4d2_kill_survivor",
	author = "X光",
	description = "特感秒杀幸存者",
	version = PLUGIN_VERSION,
	url = "QQ群59046067"
}

public void OnPluginStart()
{
	RegAdminCmd("sm_ms", Command_KillSurvivor, ADMFLAG_KICK, "插件开关.");
	C_KillSwitch = CreateConVar("l4d2_kill_survivor", "1", "0=插件关闭, 1=插件启用.", FCVAR_NOTIFY);

	HookEvent("player_hurt", Event_PlayerHurt, EventHookMode_Pre);
	HookEvent("player_incapacitated",	Event_PlayerHurt, EventHookMode_Pre);  //玩家倒地事件	

}

public Action Command_KillSurvivor(int client, int args)
{
	C_KillSwitch.BoolValue = !C_KillSwitch.BoolValue;
	PrintToChat(client, "\x04[提示]\x05特感秒杀幸存者功能已\x03%s\x05.", C_KillSwitch.BoolValue ? "开启" : "关闭");

	return Plugin_Handled;
}

bool IsValidClient(int client)
{
	return client > 0 && client <= MaxClients && IsClientInGame(client);
}

char[] GetTrueName(int client)
{
	char sName[32];
	int Bot = IsClientIdle(client);

	if(Bot != 0)
		FormatEx(sName, sizeof(sName), "闲置:%N", Bot);
	else
		GetClientName(client, sName, sizeof(sName));
	return sName;
}

int IsClientIdle(int client)
{
	if (!HasEntProp(client, Prop_Send, "m_humanSpectatorUserID"))
		return 0;

	return GetClientOfUserId(GetEntProp(client, Prop_Send, "m_humanSpectatorUserID"));
}

char[] GetPlayerName(int client, int iHLZClass)
{
	char sName[32];
	GetClientName(client, sName, sizeof(sName));

	if (!IsFakeClient(client))
	{
		Format(sName, sizeof(sName), "%s\x04%s", g_sZombieName[iHLZClass], sName);
	}
	else
	{
		SplitString(sName, g_sZombieClass[iHLZClass], sName, sizeof(sName));
		Format(sName, sizeof(sName), "%s%s", g_sZombieName[iHLZClass], sName);
	}
	return sName;
}

void Event_PlayerHurt(Event event, const char[] name, bool dontBroadcast)
{
	if(!C_KillSwitch.BoolValue)
		return;

	int victim = GetClientOfUserId(event.GetInt("userid"));
	int attacker = GetClientOfUserId(event.GetInt("attacker"));

	if(IsValidClient(victim) && GetClientTeam(victim) == 2)
	{
		if(IsValidClient(attacker) && GetClientTeam(attacker) == 3)
		{
			int iHLZClass = GetEntProp(attacker, Prop_Send, "m_zombieClass") - 1;
			PrintToChatAll("\x04[提示]\x03%s\x05杀死了\x03%s\x05.", GetPlayerName(attacker, iHLZClass), GetTrueName(victim));
			ForcePlayerSuicide(victim);
		}
	}
}



